

clear
set more 1
set type double

capture log using "log/export_share.log", replace
*****************************************************************
/*NOTES:
*construct export share = xeu exports/production and split into euro vs noneuro currency at firm-level to be used in import currency choice (analogous to phi_xeu in export currency choice)
*creates additional exports_production_missing for missing export data
*
*1. use currency export data to split between xeuro and other
*combine exports by currency data and split xeuro exports into euro currency and other.
*currency17_18 also includes euro trade so drop that

*2. we do not fill in missing currency, but instead construct additional variable of missing curency exports, which we include as a control.
*
*3. combine xvalue_xeu and currency split data with production data.
*construct share of exorts in production by year and then take the average
*construct an over exshare indicator for firms that have all obs above 1 and drop these later
*
*4. combine data with imports
*******************************************************************************************/

*1. construct exports by currency
**********************************************

use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
	drop *I
	rename country cty_code
	rename valueX value
	drop if missing(value)
    	drop if missing(currency)
    	gen euro_dum= currency=="EUR"
    	    	
    	gen value_euro=value*euro_dum
    	gen value_other=value*(1-euro_dum)
    
    	collapse (sum) value*, by(vat cty_code )
    	
    	gen share_euro_fc=value_euro/value
    	gen share_other_fc=value_other/value
     	    	    	
        label var share_euro_fc "share of exports in 2017-20 in euro by country"
        
 save temp/Xcurrency_vat, replace   
  
    	
**********************************************
*2. Construct xshare
**********************************
*Construct xshare variable as average for 2017-2019
****************************************************
****************************************

*i. construct extra-eurozone exports by firm
*********************************************
use ../../rawdata/X_2019, clear 
	append using ../../rawdata/X_2018 
	append using ../../rawdata/X_2017

*clean data
	drop if land=="EU"
	collapse (sum) value, by(vat land month year flow)
		
	gen eurozone = inlist(land, "AT", "CY", "EE", "FI", "FR", "DE", "GR", "IE", "IT") 
	replace eurozone =1 if inlist(land, "LV", "LT", "LU", "MT", "NL", "PT", "SK", "SI", "ES")
	replace eurozone = 1 if inlist(land, "AD", "MC", "SM", "XK", "ME") 
	rename land cty_code
	
	gen value_eu=value*eurozone
	gen value_xeu=value*(1-eurozone)
	
	merge m:1 cty_code using ../data/usd_peg, keep(match master) nogen
	gen value_peg=value*(usd_peg==1)

preserve
	collapse (sum) value*, by(vat year)
	
	save temp/xvalue_vat_year, replace
	
	collapse (mean) value*, by(vat)
	save temp/xvalue_vat, replace
restore	
preserve
	collapse (sum) value, by(cty_code vat year)
	collapse (mean) xvalue_fd=value, by(cty_code vat)
	label var xvalue "export value by firm destination"
	save temp/xvalue_fd, replace
restore
	merge m:1 vat cty_code using temp/Xcurrency_vat, keep(match master)
		
	tab _merge
	rename _merge merge
	gen xvalue_xeu_missing=value_xeu if missing(share_euro_fc)|missing(share_other_fc)
	
*we want to split value_xeu by share in euro
	gen xvalue_xeu_euro=value_xeu*share_euro_fc
	gen xvalue_xeu_other=value_xeu*share_other_fc
	gen xvalue_xeu=value_xeu
	
	collapse (sum) xvalue_xeu*, by(vat year) 
	drop if xvalue_xeu==0
save temp/xeuro_exports_currency, replace 

*ii. collapse turnover data
*********************************
use ../../rawdata/vat_declaration_monthly_updated, clear
	keep if year==2017|year==2018
	collapse (sum) turnover, by(vat year)
save temp/turnover_2017to2018, replace


*add in	2019 
use ../../rawdata/update_feb21/monthly&quarterly_salesVAT_2019_2020.dta, clear
	keep if year==2019
	collapse (sum) turnover, by(vat year)
	append using temp/turnover_2017to2018
save temp/turnover, replace

*/
*iii. combine currency and turnover data
***************************************************

use temp/xeuro_exports_currency
	merge 1:1 vat year using temp/turnover, keep(match) nogen
	drop if missing(turnover)|turnover==0
		drop if turnover<0
		gen xshare_xeu=xvalue_xeu/turnover
		gen xshare_xeu_euro=xvalue_xeu_euro/turnover
		gen xshare_xeu_other=xvalue_xeu_other/turnover
		gen xshare_xeu_missing=xvalue_xeu_missing/turnover
		
		gen over=xshare_xeu>=1 & !missing(xshare_xeu)
		drop if over==1
		keep vat year xshare_xeu*
		collapse (mean) mxshare_xeu_euro=xshare_xeu_euro mxshare_xeu_other=xshare_xeu_other mxshare_xeu=xshare_xeu mxshare_xeu_missing=xshare_xeu_missing, by(vat)
		label var mxshare_xeu_euro "average of xshare_xeu_euro between 2017 and 2018"					
	
	save temp/xshare_xeu_currency, replace	
	

	
***********************************

*3. combine with monthly importer data for currency choice regs	
use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
	drop *X
	rename country land
	rename valueI value
	drop if missing(value)
	drop if missing(currency)
	
	rename land cty_code
	drop if cty_code=="EU"
	
*work out which obs to drop and which are xshare zero.
	collapse (sum) mvalue_tot=value, by(vat)
	merge 1:1 vat using temp/xshare_xeu_currency
	merge 1:1 vat using temp/xvalue_vat, keep(match master) nogen
	
	replace mxshare_xeu=0 if missing(value_xeu)|value_xeu==0
	replace mxshare_xeu_euro=0 if mxshare_xeu==0
	replace mxshare_xeu_other=0 if mxshare_xeu==0
	replace mxshare_xeu_missing=0 if mxshare_xeu==0
	
*drop all obs for which xshare_xeu is missing but value_xeu>0
	drop if missing(mxshare_xeu) & value_xeu~=0
	
	keep vat mxshare* 
	
save ../data/xshare_xeu_currency, replace		



